package com.rcdz.mykotlindemo.view.customview.ohter

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import android.view.animation.RotateAnimation
import kotlin.random.Random


/**
 * 自定义转盘
 */
class TurntableView:View,View.OnClickListener{
    private lateinit var mContext: Context;
    //构造参数
    constructor(context: Context,attributeSet: AttributeSet): super(context,attributeSet)
    val mstr = "开始"
    private val contents = arrayOf("美 女", "女 神", "热 舞", "丰 满", "性 感", "知 性")
    var colors = intArrayOf(Color.parseColor("#8EE5EE"), Color.parseColor("#FFD700"), Color.parseColor("#FFD39B"), Color.parseColor("#FF8247"), Color.parseColor("#FF34B3"), Color.parseColor("#F0E68C"))
    private var mWidth = 0
    private var mPaint: Paint? = null
    private var startjs = 0f
    init {
        mPaint = Paint()
        setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        //随机数
        //随机数
        val js = Random.nextInt(3240)
        val last = Random.nextInt(360) + 360
        //旋转动画
        //旋转动画
        val rotateAnimation = RotateAnimation(
            startjs,
            (js + last).toFloat(), (mWidth / 2).toFloat(), (mWidth / 2).toFloat()
        )
        //旋转时间
        //旋转时间
        rotateAnimation.duration = 3000
        //保留最后执行完的位置
        //保留最后执行完的位置
        rotateAnimation.fillAfter = true
        //启动动画
        //启动动画
        startAnimation(rotateAnimation)
        startjs = ((js + last) % 360).toFloat()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        mWidth = measuredWidth
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        mPaint?.color=Color.YELLOW
        mPaint?.style=Paint.Style.STROKE
        mPaint?.isAntiAlias = true
        mPaint?.strokeWidth = 3F
        canvas?.drawCircle(mWidth/2F,mWidth/2F,mWidth/2F, mPaint!!)
        println("mWidth=="+mWidth)
        var rectF:RectF = RectF(0F,0F, mWidth.toFloat(), mWidth.toFloat())
        mPaint?.style = Paint.Style.FILL
        for (index in colors.indices){
            //给扇形填充颜色
            mPaint?.color = colors[index]
            var startjiaodu = index*60F
            //画扇形 开始角度，结束角度，是否有中心，画笔
            canvas?.drawArc(rectF,startjiaodu,60F,true, mPaint!!)
        }
        //字体颜色
        mPaint?.color=Color.BLACK
        //字体大小
        mPaint?.textSize = 24F
        //进行循环
        for (index in contents.indices){
            var startjiaodu = index*60F
            //设置文字的路径
            var path = Path();
            path.addArc(rectF,startjiaodu,60F)
            canvas?.drawTextOnPath(contents[index],path,50F,50F,mPaint!!)
        }
        mPaint?.color = Color.RED
        canvas!!.drawCircle((mWidth / 2).toFloat(), (mWidth / 2).toFloat(), 50F, mPaint!!)
        mPaint!!.color = Color.BLUE
        mPaint!!.textSize = 24f
        //要得到我们写的字的高和宽
        //要得到我们写的字的高和宽
        val rect = Rect()
        mPaint?.getTextBounds(mstr, 0, mstr.length, rect)
        val width = rect.width()
        val height = rect.height()
        canvas.drawText(
            mstr, (mWidth / 2 - width / 2).toFloat(),
            (mWidth / 2 + height / 2).toFloat(), mPaint!!
        )
    }

}